import React from 'react'
import { Avatar, Button } from 'antd';

const UserList = ['一', '一缕', '一缕清', '一缕清风'];
const colorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae'];

export default class Autoset extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      user: UserList[0],
      color: colorList[0],
    };
  }

  componentDidMount() {
    setInterval(() => {
      const index = UserList.indexOf(this.state.user);
      this.setState({
        user: index < UserList.length - 1 ? UserList[index + 1] : UserList[0],
        color: index < colorList.length - 1 ? colorList[index + 1] : colorList[0],
      });
    }, 500);
  }

  handleChangeUser = () => {
    const index = UserList.indexOf(this.state.user);
    this.setState({
      user: index < UserList.length - 1 ? UserList[index + 1] : UserList[0],
      color: index < colorList.length - 1 ? colorList[index + 1] : colorList[0],
    });
  };

  render() {
    return (
      <div>
        <Avatar style={{ backgroundColor: this.state.color, verticalAlign: 'middle' }} size="large">
          {this.state.user}
        </Avatar>
        <Button
          size="small"
          style={{ marginLeft: 16, verticalAlign: 'middle' }}
          onClick={this.handleChangeUser}
        >
          Change
        </Button>
      </div>
    );
  }
}